package com.czl.module_storehouse.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import com.czl.base.util.SpHelper;
import com.czl.module_base.bean.ListBean;
import com.czl.module_base.databinding.CommonSearchViewBinding;
import com.czl.module_base.fragment.BaseRecyclerViewFragment;
import com.czl.module_base.mvp.presenter.InjectPresenter;
import com.czl.module_service.event.AllocateHomeEvent;
import com.czl.module_service.system.Constants;
import com.czl.module_storehouse.R;
import com.czl.module_storehouse.activity.allocate.add.AllocateAddActivity;
import com.czl.module_storehouse.activity.allocate.details.AllocateDetailsActivity;
import com.czl.module_storehouse.activity.allocate.post.AllocatePostInActivity;
import com.czl.module_storehouse.activity.allocate.post.AllocatePostOutActivity;
import com.czl.module_storehouse.adapter.AllocateAdapter;
import com.czl.module_storehouse.bean.AllocateBean;
import com.czl.module_storehouse.databinding.CommonButtonBinding;
import com.czl.module_storehouse.event.AllocateEvent;
import com.czl.module_storehouse.event.AllocatePostInEvent;
import com.czl.module_storehouse.mvp.presenter.AllocatePresenter;
import com.czl.module_storehouse.mvp.view.AllocateView;
import com.jeremyliao.liveeventbus.LiveEventBus;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;

/**
 * @author xubin
 */
public class AllocateFragment extends BaseRecyclerViewFragment<CommonSearchViewBinding> implements AllocateView {

    private int mState;
    private AllocateAdapter mAdapter;

    @InjectPresenter
    AllocatePresenter mAllocatePresenter;
    private boolean loading;

    public static AllocateFragment newInstance(int state) {
        Bundle args = new Bundle();
        args.putInt("state", state);
        AllocateFragment fragment = new AllocateFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected CommonSearchViewBinding getBinding(LayoutInflater layoutInflater, ViewGroup container) {
        return CommonSearchViewBinding.inflate(layoutInflater);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);
        Bundle arguments = getArguments();
        if (arguments != null) {
            mState = arguments.getInt("state");
        }
        if (mState == 0) {
            CommonButtonBinding buttonBinding = CommonButtonBinding.inflate(getLayoutInflater(), mRecyclerViewBinding.containerBottom, true);
            buttonBinding.button.setText("添加调拨单");
            buttonBinding.button.setOnClickListener(v -> startActivity(new Intent(requireContext(), AllocateAddActivity.class)));
        }
        mAdapter = new AllocateAdapter(R.layout.item_allocate, new ArrayList<>());
        mAdapter.setOnItemChildClickListener((adapter, view, position) ->
                goActivity(mAdapter.getItem(position)));
        mRecyclerViewBinding.recyclerView.setAdapter(mAdapter);
        LiveEventBus.get(AllocateHomeEvent.class).observeSticky(this, event -> {
            if (event != null && !loading) {
                reload();
            }
        });
    }

    @Subscribe(threadMode = ThreadMode.POSTING, sticky = true)
    public void onDataEvent(AllocatePostInEvent event) {
        reload();
    }

    @Override
    protected void reload() {
        super.reload();
        loading = true;
        loadData(true);
    }

    @Override
    protected void loadMore() {
        super.loadMore();
        loadData(false);
    }

    private void loadData(boolean refresh) {
        if (mState == 0) {
            if (mAllocatePresenter != null) {
                mAllocatePresenter.getNeedDealtMoveList(refresh);
            }
        } else {
            if (mAllocatePresenter != null) {
                mAllocatePresenter.getDealtMoveList(refresh);
            }
        }
    }

    @Override
    public void hideDialogLoading() {
        super.hideDialogLoading();
        loading = false;
    }

    @Override
    public <T> void showData(T t) {
        super.showData(t);
        handleResponseData(mAdapter, mAllocatePresenter.getPageNo(), (ListBean<AllocateBean>) t);
    }

    private void goActivity(AllocateBean bean) {
        AllocateEvent allocateEvent = new AllocateEvent(bean);
        EventBus.getDefault().postSticky(allocateEvent);
        if (mState == 0) {
            int storehouseId = SpHelper.INSTANCE.decodeInt(Constants.SpKey.STOREHOUSE_ID);
            if (storehouseId == bean.getInStorehouseId()) {
                //入库
                startActivity(new Intent(getContext(), AllocatePostInActivity.class));
            } else if (storehouseId == bean.getMoveStorehouseId()) {
                //出库
                startActivity(new Intent(getContext(), AllocatePostOutActivity.class));
            }
        } else {
            startActivity(new Intent(getContext(), AllocateDetailsActivity.class));
        }
    }
}
